package com.sample.springbatch.job.splitfile;

import com.sample.springbatch.config.BaseConfig;
import com.sample.springbatch.exception.MySkipException;
import com.sample.springbatch.job.sample.service.RepayOrderBackToBackPatternClassifier;
import com.sample.springbatch.model.RepayOrderInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.FlatFileParseException;
import org.springframework.batch.item.file.LineCallbackHandler;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.support.ClassifierCompositeItemWriter;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.task.SyncTaskExecutor;

/**
 * 分割文件作业
 * @author zwg
 */

@Slf4j
@Configuration
public class SplitFileJobConfig {




    @Bean
    public FlatFileItemReader splitFileJobReader(){

        FlatFileItemReader reader = new FlatFileItemReader();
        reader.setResource(new ClassPathResource("jobdata/repay-orders-big.dat"));
        //# 为默认的注释符号
        reader.setComments(new String[] { "==","##" });
        reader.setLinesToSkip(1);
        reader.setSkippedLinesCallback(new LineCallbackHandler() {
            @Override
            public void handleLine(String s) {
                log.error("skip line:"+s);
            }
        });
        reader.setLineMapper(new DefaultLineMapper<RepayOrderInfo>() {{
            //一行内容分割默认用','分割
            setLineTokenizer(new DelimitedLineTokenizer("|") {{
                setNames(new String[] { "userId", "productId","contractNo","amount","repayType","repayMethod" });
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<RepayOrderInfo>() {{
                setTargetType(RepayOrderInfo.class);
            }});


        }});
        return reader;
    }


    @Bean
    public ItemProcessor splitFileJobProcessor(){
        ItemProcessor processor = new ItemProcessor<RepayOrderInfo,RepayOrderInfoZone>() {
            @Override
            public RepayOrderInfoZone process(RepayOrderInfo o) throws Exception {
                RepayOrderInfoZone zone = new RepayOrderInfoZone();
                BeanUtils.copyProperties(o,zone);
                //TODO 设置分区值
                zone.setZone(o.getRepayMethod()+"");

                return zone;
            }
        };
        return processor;
    }

    @Bean
    public ClassifierCompositeItemWriter splitFileJobWriter(){
        ClassifierCompositeItemWriter writer = new ClassifierCompositeItemWriter();
        writer.setClassifier(new RepayOrderBackToBackPatternClassifier());
        return writer;
    }



    @Bean(name = "repayOrderInfoZoneJob")
    public Job repayOrderInfoZoneJob(JobBuilderFactory jobBuilderFactory,
                                     StepBuilderFactory stepBuilderFactory) {
        return jobBuilderFactory.get("repayOrderInfoZoneJob")
                .incrementer(new RunIdIncrementer())
                //增加对job的监听
                //.listener(myJobListener)
                .flow(repayOrderInfoZoneJobStep(stepBuilderFactory))
                .end()
                .build();
    }

    @Bean(name="repayOrderInfoZoneJobStep")
    public Step repayOrderInfoZoneJobStep(StepBuilderFactory stepBuilderFactory) {
        return stepBuilderFactory.get("repayOrderInfoZoneJobStep")
                .<RepayOrderInfo, RepayOrderInfoZone> chunk(10)
                .reader(splitFileJobReader())
                .processor(splitFileJobProcessor())
                //路由writer
                .writer(splitFileJobWriter())
                //.stream(repayOrderFlatFileItemWriter)  //路由writer stream 必须添加些文件的writer
                //.stream(repayOrderNewFlatFileItemWriter)
                //.stream(repayOrderMultiResourceFlatFileItemWriter)
                //.stream(repayOrderNewMultiResourceFlatFileItemWriter)
                .build();
    }

}
